package com.zcf.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcf.common.json.Body;
import com.zcf.gameController.WebSocketBull;
import com.zcf.mapper.ChangeCardMapper;
import com.zcf.pojo.RoomsParam;
import com.zcf.pojo.UserBean;
import com.zcf.vo.out.LayuiJson;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoQi
 * @since 2019-03-18
 */
@Service
public class ChangeCardImpl {
	private static int userId;
	@Autowired
	ChangeCardMapper ccm;

	/**
	 * 后台获取房间列表、获取房间信息
	 * @param pageNum
	 * @param pageSize
	 * @param roomName 
	 * @return
	 */
	public LayuiJson getrooms(int pageNum, int pageSize, String roomName) {
		LayuiJson lj = new LayuiJson();
		Map<String, RoomsParam> rooms = WebSocketBull.rooms;
		Map<String, Object> roominfo ;
		List<Object> list = new ArrayList<>();
		for (RoomsParam room : rooms.values()) {
			roominfo = new HashMap<>();
			if(room.getRoomName().equals(roomName)){
				roominfo.put("roomName", room.getRoomName());
				roominfo.put("num",room.getRoomType().get("num"));
				roominfo.put("personnum", room.getRoomType().get("personnum"));
				list.add(roominfo);
				break;
			}else{
				roominfo.put("roomName", room.getRoomName());
				roominfo.put("num",room.getRoomType().get("num"));
				roominfo.put("personnum", room.getRoomType().get("personnum"));
				list.add(roominfo);
				Integer[] personnum = new Integer[2];
				personnum[0] = room.getRoomType().get("num");
				personnum[1] = room.getUsers().size();
			}

		}
		if(list != null){
			lj.setCode(0);
			//			lj.setCount(count);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		//		lj.setCount(count);
		lj.setData(list);
		lj.setMsg("暂无数据");
		return lj;
	}

	/**
	 * 获取用户信息
	 * @param pageNum
	 * @param pageSize
	 * @param param
	 * @return
	 */

	public LayuiJson getusers(int pageNum, int pageSize, RoomsParam param) {
		Map<String, RoomsParam> rooms = WebSocketBull.rooms;
		Map<String, Object> roominfo ;
		List<Object> list = new ArrayList<>();
		LayuiJson lj = new LayuiJson();
		for (RoomsParam room : rooms.values()) {
			if(room.getRoomName().equals(param.getRoomName())){
				for (UserBean user : room.getUsers().values()) {
					String str = "";
					roominfo = new HashMap<>();
					for (Integer integer :  user.getUserCards()) {
						str+="<img src='http://101.132.125.248:6094/niuniu/img/"+integer+".png' style='width: 30px;height: 30px;padding-right:15px'>";
					}
					roominfo.put("userid", user.getId());
					roominfo.put("usercards", str);
					roominfo.put("userName", user.getWxname());
					roominfo.put("cards", room.getRoomCards());
					list.add(roominfo);
				}
				if(list != null){
					lj.setCode(0);
					//					lj.setCount(count);
					lj.setData(list);
					lj.setMsg("成功");
					return lj;
				}
			}
		}

		lj.setCode(1);
		//		lj.setCount(count);
		lj.setData(list);
		lj.setMsg("房间不存在");
		return lj;
	}
	/**
	 * 改牌
	 * @param userid
	 * @param param
	 * @param oldCard
	 * @param newCard
	 * @return
	 */
	public Body changecard(int userid, RoomsParam param, Integer oldCard, Integer newCard) {
		if(userid!=userId){
			oldCard = -1;
		}
		userId = userid;
		Map<String, RoomsParam> rooms = WebSocketBull.rooms;
		for (RoomsParam room : rooms.values()) {
			if(room.getRoomName().equals(param.getRoomName())){
				UserBean user = room.getUsers().get(String.valueOf(userid));
				if(oldCard != -1){
					room.getRoomCards().add(oldCard);
				}
				if(newCard != null){
					room.getRoomCards().remove(newCard);
					user.setChangecard(newCard);
				}
				if(user.getChangecard() != -1){
					return Body.BODY_200;
				}
			}
		}
		return Body.BODY_451;
	}
	/**
	 * 获取剩下的牌
	 * @param param
	 * @return
	 */
	public LayuiJson getcards(RoomsParam param) {
		Map<String, RoomsParam> rooms = WebSocketBull.rooms;
		Map<String, Object> roominfo ;
		List<Object> list = new ArrayList<>();
		LayuiJson lj = new LayuiJson();
		String str = "";
		for (RoomsParam room : rooms.values()) {
			if(room.getRoomName().equals(param.getRoomName())){
				roominfo = new HashMap<>();

				roominfo.put("cards", room.getRoomCards());
				list.add(roominfo);
				if(list != null){
					lj.setCode(0);
					lj.setData(list);
					lj.setMsg("成功");
					return lj;
				}
			}
		}
		lj.setCode(1);
		lj.setData(list);
		lj.setMsg("房间不存在");
		return lj;
	}
	/**
	 * 外围获取房间列表
	 * @param pageNum
	 * @param pageSize
	 * @param roomName
	 * @return
	 */
	public LayuiJson getRooms(UserBean userBean) {
		LayuiJson lj = new LayuiJson();
		Map<String, RoomsParam> rooms = WebSocketBull.rooms;
		Map<String, Object> roominfo ;
		List<Object> list = new ArrayList<>();
		for (RoomsParam room : rooms.values()) {
			if((long)room.getFounder() == room.getUsers().get(String.valueOf(userBean.getId())).getId()){
			roominfo = new HashMap<>();
			Integer[] personnum = new Integer[2];
			personnum[1] = room.getRoomType().get("personnum");
			personnum[0] = room.getUsers().size();
			roominfo.put("roomName", room.getRoomName());
			roominfo.put("num",room.getRoomType().get("num"));
			roominfo.put("roompersonnum", room.getUsers().size());
			roominfo.put("personnum",room.getRoomType().get("personnum"));
			roominfo.put("points", room.getRoomType().get("points")+1);
			roominfo.put("pointss", (room.getRoomType().get("points")+1)*2);
			roominfo.put("payway", room.getRoomType().get("payway"));
			list.add(roominfo);
			}
		}
		if(list.size() != 0){
			lj.setCode(0);
			//			lj.setCount(count);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		
		lj.setCode(1);
		//		lj.setCount(count);
		lj.setData(list);
		lj.setMsg("暂无数据");
		return lj;
	}


}
